import { http } from '@/utils/axios/init'
//查询目录树
export function getArchiveFolder(data) {
  return http.request({
    url: `/common/Rest/projectArchiveFolder/tree`,
    method: 'get',
    params: data
  })
}
//查询目录树(ctms)
export function getCtmsArchiveFolder(data) {
  return http.request({
    url: `/ctms/Rest/projectArchiveFolder/tree`,
    method: 'get',
    params: data
  })
}
export function getArchiveFolderByProjectType(data) {
  return http.request({
    url: `/common/Rest/sop/projectFolderTemplate/getByProjectType`,
    method: 'get',
    params: data
  })
}
//新增目录
export function saveProjectArchiveFolder(params) {
  return http.request(
    {
      url: `/common/Rest/projectArchiveFolder/save`,
      method: 'post',
      params
    },
    {
      successMessageText: '新增成功'
    }
  )
}
//编辑目录
export function updateProjectArchiveFolder(params) {
  return http.request(
    {
      url: `/common/Rest/projectArchiveFolder/update`,
      method: 'post',
      params
    },
    { successMessageText: '操作成功' }
  )
}
//删除目录
export function deleteProjectArchiveFolder(params) {
  return http.request(
    {
      url: `/common/Rest/projectArchiveFolder/delete`,
      method: 'post',
      params
    },
    { joinParamsToUrl: true, successMessageText: '删除成功' }
  )
}
//目录移动
export function moveDeleteProjectArchiveFolder(params) {
  return http.request({
    url: `/common/Rest/projectArchiveFolder/move`,
    method: 'post',
    params
  })
}
//查询文件分页列表
export function getArchiveFileList(data) {
  return http.request({
    url: `/common/Rest/projectArchiveFile/listByPage`,
    method: 'get',
    params: data
  })
}
//文件上传
export function uploadArchiveFile(params) {
  return http.request(
    {
      url: `/common/Rest/projectArchiveFile/upload`,
      method: 'post',
      params
    },
    { successMessageText: '操作成功' }
  )
}
//移动
export function batchMove(params) {
  return http.request(
    {
      url: `/common/Rest/projectArchiveFile/move`,
      method: 'post',
      params
    },
    { successMessageText: '操作成功' }
  )
}
//批量移动
export function mutilBatchMove(params) {
  return http.request(
    {
      url: `/common/Rest/projectArchiveFile/batchMove`,
      method: 'post',
      params
    },
    { successMessageText: '操作成功' }
  )
}
//查询文件历史
export function getArchiveFileHistory(data) {
  return http.request({
    url: `/common/Rest/projectArchiveFile/history`,
    method: 'get',
    params: data
  })
}
//更新文件
export function updateArchiveFile(params) {
  return http.request(
    {
      url: `/common/Rest/projectArchiveFile/update`,
      method: 'post',
      params
    },
    {
      successMessageText: '操作成功'
    }
  )
}
//删除文件
export function deleteArchiveFile(data) {
  return http.request(
    {
      url: `/common/Rest/projectArchiveFile/delete`,
      method: 'post',
      params: data
    },
    {
      joinParamsToUrl: true,
      successMessageText: '删除成功'
    }
  )
}

//目录文件打包下载数据封装
export function encapsulationDownload(params) {
  return http.request({
    url: `/common/Rest/projectArchiveFolder/encapsulationDownload`,
    method: 'get',
    params
  })
}

//目录层级批量下载
export function folderClassBatchDowload(params) {
  return http.request({
    url: `/doc/Rest/doc/file/folderClassBatchDowload`,
    method: 'post',
    params
  })
}

// 文件借阅列表
export function getBorrowList(data) {
  const url = data.projectId ? 'listByProjectPage' : 'listByOrgPage'
  return http.request({
    url: `/common/Rest/fileBorrow/${url}`,
    method: 'get',
    params: data
  })
}

// 新增文件借阅记录
export function editBorrow(data) {
  if (!data.projectId) {
    data.projectId = sessionStorage.getItem('__projectId')
  }
  console.log('data', data)
  const type = data.id ? 'update' : 'add'
  return http.request(
    {
      url: `/common/Rest/fileBorrow/${type === 'add' ? 'draft' : 'update'}`,
      method: 'post',
      data
    },
    {
      successMessageText: type === 'update' ? '操作成功' : undefined
    }
  )
}

// 删除文件借阅
export function deleteBorrow(id) {
  return http.request(
    {
      url: `/common/Rest/fileBorrow/delete`,
      method: 'post',
      data: { id }
    },
    {
      successMessageText: '操作成功'
    }
  )
}

// 借阅审批
export function confirmBorrow(data) {
  return http.request(
    {
      url: `/common/Rest/fileBorrow/examine`,
      method: 'post',
      data
    },
    {
      successMessageText: '操作成功'
    }
  )
}

// 设为归还
export function returnBorrow(data) {
  return http.request(
    {
      url: `/common/Rest/fileBorrow/restitution`,
      method: 'post',
      data
    },
    {
      successMessageText: '操作成功'
    }
  )
}

// 文件借阅导出
export function downloadBorrowList(data) {
  return http.request(
    {
      url: `/common/Rest/fileBorrow/export`,
      method: 'post',
      data,
      responseType: 'blob'
    },
    {
      isReturnNativeResponse: true,
      successMessageText: '操作成功'
    }
  )
}

// 查询借阅详情
export function getBorrowDetail(id) {
  return http.request({
    url: `/common/Rest/fileBorrow/details`,
    method: 'get',
    params: { id }
  })
}
